import React, { Component } from "react";
import { View, Text, Image, StyleSheet, TouchableWithoutFeedback, FlatList } from "react-native";
import ScrollableTabView, { ScrollableTabBar,DefaultTabBar} from 'react-native-scrollable-tab-view';
import CustomerHeader from "../../widget/customerHeader";
import PropTypes from "prop-types";

class IntegralCustomTabBar extends Component{
  constructor(props) {
    super(props);
  }
  static propTypes = {
    tabLabelNames: PropTypes.array, // tabs
    goToPage: PropTypes.func, // 跳转到对应tab的方法
    tabs: PropTypes.array,
    activeTab: PropTypes.number,
    onPress:PropTypes.func
  }
  render() {
    const containerWidth = this.props.containerWidth;
    const numberOfTabs = this.props.tabs.length;
    const translateX = this.props.scrollValue.interpolate({
      inputRange: [0, 1],
      outputRange: [0, containerWidth / numberOfTabs],
    });
    return (
      <View style={[this.props.style,styles.tabs]}>
        {this.props.tabs.map((tab,i)=>{
          return <TouchableWithoutFeedback style={styles.tabBox} key={tab} onPress={()=>this.props.goToPage(i)}>
            <View style={styles.tabBox}>
              <Text style={[styles.tab,i === this.props.activeTab?{fontSize:14,color:'#222222',fontWeight:'500'}:{}, this.props.textStyle]}>{this.props.tabLabelNames[i].label}</Text>
              {i===this.props.activeTab?<Image style={styles.lineBar}  source={require('../../img/line_bar.png')}></Image>:null}
            </View>
          </TouchableWithoutFeedback>
        })}
        <View style={styles.bottomLine}></View>
      </View>
    );
  }
}

export default class IntegralRecord extends Component{
  constructor(props) {
    super(props);
    this.tabView=null;
    this.state={
      tabList:[{
        label:'全部',
        index:1
      },{
        label:'收入',
        index:2
      },{
        label:'支出',
        index:3
      }],
      dataList:[{
        label:'1',
        value:1
      },{
        label:'2',
        value:2
      }]
    }
  }
  onChangeTab(obj){
    console.log(obj)
  }
  selectTabChange(i){
    if(this.tabView)this.tabView.goToPage(i)

  }
  _renderItem(){
    return (
      <View style={styles.cell}>
        <View>
          <Text style={{color:'#000000',fontSize:15}}>签到</Text>
          <Text style={{color:'#9B9B9B',fontSize:13,marginTop:10}}>2021-01-01 19:19:00</Text>
        </View>
        <Text style={styles.score}>+3</Text>
        <View style={styles.line}></View>
      </View>
    )
  }
  render() {
    return(
      <View style={{flex:1,backgroundColor:'#fff'}}>
        <CustomerHeader title={'收支记录'} leftOnPress={()=>{this.props.navigation.pop()}}></CustomerHeader>
        <ScrollableTabView initialPage={0} ref={(tabView) => { this.tabView = tabView; }} prerenderingsiblingsNumber={1} scrollWithoutAnimation={false} onChangeTab={this.onChangeTab} onScroll={
          (position) => {
            console.log('滑动时的位置：' + position);
          }
        } locked={false} renderTabBar={()=><IntegralCustomTabBar tabLabelNames={this.state.tabList} onPress={this.selectTabChange}/>}>
          <View tabLabel='Tab1' style={{flex: 1}}>
            <View style={{flex:1 ,width:'100%'}}>
              <FlatList data={this.state.dataList} renderItem={this._renderItem}></FlatList>
            </View>
          </View>
          <View tabLabel='Tab2' style={{flex: 1}}>
            <View style={{flex:1 ,width:'100%'}}>
              <Text>界面4</Text>
            </View>
          </View>
          <View tabLabel='Tab3' style={{flex: 1}}>
            <View style={{flex:1 ,width:'100%'}}>
              <Text>界面5</Text>
            </View>
          </View>
        </ScrollableTabView>
      </View>
    )
  }
}

const styles=StyleSheet.create({
  /**tab**/
  tabs:{
    height:40,
    backgroundColor:'#fff',
    flexDirection:'row',
    justifyContent:'space-around',
    alignItems:'center',
  },
  tabBox:{
    height:40,
    justifyContent: 'center',
    alignItems:'center'
  },
  tab:{
    color: '#666666',
    fontSize: 14,
    paddingLeft: 10,
    paddingRight:10
  },
  lineBar:{
    position:'absolute',
    width:20,
    height:3,
    bottom:0,
  },
  cell:{
    backgroundColor: '#fff',
    height:73,
    flexDirection: 'row',
    justifyContent:'space-between',
    alignItems:'center',
    paddingHorizontal:16
  },
  score:{
    color:'#E39354',
    fontSize:14
  },
  line:{
    position: 'absolute',
    left:16,
    right:16,
    bottom:0,
    height:1,
    backgroundColor:'#F6F6F6'
  },
  bottomLine:{
    position: 'absolute',
    left:0,
    right:0,
    bottom:0,
    height:1,
    backgroundColor:'#F6F6F6'
  }
})
